package com.huike.game.Activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.huike.game.R;
import com.huike.game.base.Game62;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;

import androidx.appcompat.app.AppCompatActivity;

public class GameDescActivity62 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private ListView lv_game62;
    private ArrayList<Game62.DataBean> list;
    private ViewList viewList;
    private TextView tv_game62_title;
    private ImageView tv_game62_cut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_desc62);
        list = new ArrayList<>();
        EventBus.getDefault().register(this);
        initView();
        viewList = new ViewList();
        lv_game62.setAdapter(viewList);

        final String name = list.get(0).getName();//路线首
        final String name1 = list.get(list.size() - 1).getName();//路线尾

        tv_game62_title.setText(name + "-" + name1);

        //切换图片点击
        tv_game62_cut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //将数据倒序

                Collections.reverse(list);
                tv_game62_title.setText(name1 + "-" + name);
                viewList.notifyDataSetChanged();

            }
        });

    }


    //使用EventBus接收数据
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void EventBusMessage(ArrayList<Game62.DataBean> list) {

        this.list = list;

    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        lv_game62 = findViewById(R.id.lv_game62);
        tv_game62_title = findViewById(R.id.tv_game62_title);
        tv_game62_cut = findViewById(R.id.tv_game62_cut);

        iv_left_image.setImageResource(R.drawable.tiao);
        iv_rigth_image.setVisibility(View.GONE);
        tv_content_title.setText("1路");
        tv_right_text.setVisibility(View.GONE);
        tv_left_text.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    class ViewList extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(GameDescActivity62.this, R.layout.lv_game62, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Boolean aBoolean = list.get(position).getClick();
            if(aBoolean){
                TextView id = holder.ll_game62_all.getChildAt(0).findViewById(R.id.tv_game62_id);
                TextView name = (TextView) holder.ll_game62_all.getChildAt(1);
                id.setBackgroundResource(R.drawable.round_red);
                name.setTextColor(Color.parseColor("red"));

            }else{
                TextView id = holder.ll_game62_all.getChildAt(0).findViewById(R.id.tv_game62_id);
                TextView name = (TextView) holder.ll_game62_all.getChildAt(1);
                id.setBackgroundResource(R.drawable.round);
                name.setTextColor(Color.parseColor("#ff000000"));
            }

            holder.tv_game62_id.setText(list.get(position).getPlatform() +"");
            holder.tv_game62_name.setText(list.get(position).getName());

            holder.ll_game62_all.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //单选
                    list.get(position).setClick(true);
                    for (int i = 0; i < list.size(); i++) {
                        if(i != position){//不是当前点击的全部改为false
                            list.get(i).setClick(false);
                        }
                    }

//                    多选
//                    list.get(position).setClick(true);

//                    全选
//                    for (int i = 0; i < list.size(); i++) {
//                        list.get(i).setClick(true);
//                    }

                    viewList.notifyDataSetChanged();


                }
            });


            return convertView;
        }

        class ViewHolder {
            public View rootView;
            public TextView tv_game62_id;
            public TextView tv_game62_name;
            public LinearLayout ll_game62_all;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.tv_game62_id = (TextView) rootView.findViewById(R.id.tv_game62_id);
                this.tv_game62_name = (TextView) rootView.findViewById(R.id.tv_game62_name);
                this.ll_game62_all = (LinearLayout) rootView.findViewById(R.id.ll_game62_all);
            }

        }
    }
}
